package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.opportunity.pojo.SaleStageSyncStatusPojo;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.SaleStageSyncStatusDao;
import com.xbongbong.saas.domain.entity.SaleStageSyncStatusEntity;
import com.xbongbong.saas.enums.dictionary.SaleStageSyncStatusEnum;
import com.xbongbong.saas.model.SaleStageSyncStatusModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/2/14 12:19
 * @since v1.0
 */
@Service
public class SaleStageSyncStatusModelImpl implements SaleStageSyncStatusModel {

    @Resource
    private SaleStageSyncStatusDao saleStageSyncStatusDao;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Override
    public Integer insert(SaleStageSyncStatusEntity entity) throws XbbException {
        if (Objects.isNull(entity.getAddTime())) {
            entity.setAddTime((long) DateUtil.getInt());
        }
        if (Objects.isNull(entity.getUpdateTime())) {
            entity.setUpdateTime((long) DateUtil.getInt());
        }
        return saleStageSyncStatusDao.insert(entity);
    }

    @Override
    public Integer update(SaleStageSyncStatusEntity entity) throws XbbException {
        if (Objects.isNull(entity.getUpdateTime())) {
            entity.setUpdateTime((long) DateUtil.getInt());
        }
        return saleStageSyncStatusDao.update(entity);
    }

    @Override
    public List<SaleStageSyncStatusEntity> findEntitys(Map<String, Object> param) {
        return saleStageSyncStatusDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return saleStageSyncStatusDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public SaleStageSyncStatusEntity getByKey(Long key, String corpid) {
        return saleStageSyncStatusDao.getByKey(key, corpid);
    }

    @Override
    public SaleStageSyncStatusEntity getByStageProcessId(String corpid, Long formId, Long stageProcessId) {
        return saleStageSyncStatusDao.getByStageProcessId(corpid, formId, stageProcessId);
    }

    @Override
    public Integer save(SaleStageSyncStatusEntity saleStageSyncStatus) throws XbbException {
        if (saleStageSyncStatus.getId() != null) {
            return update(saleStageSyncStatus);
        } else {
            return insert(saleStageSyncStatus);
        }
    }

    @Override
    public void insertBatch(List<SaleStageSyncStatusEntity> list) {
        saleStageSyncStatusDao.insertBatch(list);
    }

    @Override
    public Integer deleteBatch(List<SaleStageSyncStatusEntity> stageEntityList, String corpid) {
        return saleStageSyncStatusDao.deleteBatch(stageEntityList, corpid);
    }

    /**
     * stageProcessId对应的阶段流程当前是否处于同步机会主数据的状态
     *
     * @param corpid         公司id
     * @param formId         表单id
     * @param stageProcessId 流程id
     * @return true/false 在同步中/没有在同步中
     */
    @Override
    public boolean isSyncStatusInProgress(String corpid, Long formId, Long stageProcessId) {
        SaleStageSyncStatusPojo saleStageSyncStatusPojo = null;
        String syncStatusKey = corpid + StringConstant.UNDER_LINE + formId + StringConstant.UNDER_LINE + stageProcessId;
        String syncStatusValue = paasRedisHelper.getValue(RedisPrefixConstant.SALE_STAGE_SYNC_STATUS, syncStatusKey);
        // 先从redis中获取同步的状态
        if (StringUtil.isNotEmpty(syncStatusValue)) {
            JSONObject syncStatusObj = FastJsonHelper.parseObject(syncStatusValue);
            saleStageSyncStatusPojo = JSON.parseObject(syncStatusObj.toJSONString(), SaleStageSyncStatusPojo.class);
        }
        // 如果redis中没有获取到同步状态，则从数据库中获取
        if (Objects.isNull(saleStageSyncStatusPojo)) {
            SaleStageSyncStatusEntity saleStageSyncStatus = getByStageProcessId(corpid, formId, stageProcessId);
            if (Objects.nonNull(saleStageSyncStatus)) {
                saleStageSyncStatusPojo = new SaleStageSyncStatusPojo(saleStageSyncStatus.getStatus(), saleStageSyncStatus.getExecuteTime(), saleStageSyncStatus.getFinishTime(), saleStageSyncStatus.getSyncTimes());
            }
        }
        // saleStageSyncStatusPojo不为空代表之前进行过数据同步
        if (Objects.nonNull(saleStageSyncStatusPojo)) {
            return Objects.equals(saleStageSyncStatusPojo.getStatus(), SaleStageSyncStatusEnum.PROGRESS.getCode());
        }
        return false;
    }
}
